package com.example.demo.zaqizabatest.多线程相关;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName HelloWorld
 * @Author lys4134
 * @Date 2021/09/01 09:31:22
 * @Description HelloWorld
 * @Version 1.0
 **/
@SuppressWarnings("AlibabaAvoidManuallyCreateThread")
public class HelloWorld implements Runnable {
    @SuppressWarnings("AlibabaThreadPoolCreation")
    private final static ExecutorService executorService = Executors.newSingleThreadExecutor();
    private Object object;
    private char s;

    public HelloWorld(Object object, char s) {
        this.object = object;
        this.s = s;
    }

    public static void main(String[] args) {
        Object object = new Object();
        char[] chars = "HelloWorld".toCharArray();
        for (int i = 0; i < chars.length; i++) {
            executorService.submit(new Thread(new HelloWorld(object, chars[i])));

        }


    }

//    private void print(char s){
//        synchronized (object){
//            System.out.println(s);
//        }
//    }

    @Override
    public void run() {
//        synchronized (object) {
        System.out.print(s);
        if ('d' == s) {
            System.out.println();
            executorService.shutdown();
        }
//        }
    }
}
